﻿using System;

namespace Microsoft.CSA.FeedSync.FeedSyncLibrarySL
{
    public class RSSFeedSyncItem : Microsoft.CSA.FeedSync.FeedSyncLibrarySL.FeedSyncItem
    {
        private string m_Title;
        private string m_Description;

        public RSSFeedSyncItem()
        {
        }

        public RSSFeedSyncItem(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.RSSFeed i_RSSFeed, string i_By, string i_ID, string i_Title, string i_Description) :
            this(i_RSSFeed.XDocument, i_By, i_ID, i_Title, i_Description)
        {
        }

        public RSSFeedSyncItem(System.Xml.Linq.XDocument i_XDocument, string i_By, string i_ID, string i_Title, string i_Description)
        {
            if (i_XDocument == null)
                throw new System.ArgumentException("i_XDocument must be non-null!");

            if (System.String.IsNullOrEmpty(i_ID))
                throw new System.ArgumentException("i_ID must be non-null!");

            m_XElement = new System.Xml.Linq.XElement(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_ITEM_ELEMENT_NAME);

            m_Title = i_Title;
            m_Description = i_Description;

            if (!System.String.IsNullOrEmpty(m_Title))
            {
                System.Xml.Linq.XElement TitleXElement = new System.Xml.Linq.XElement(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_TITLE_ELEMENT_NAME);
                TitleXElement.Value = i_Title;
                m_XElement.Add(TitleXElement);
            }

            if (!System.String.IsNullOrEmpty(m_Description))
            {
                System.Xml.Linq.XElement DescriptionXElement = new System.Xml.Linq.XElement(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_DESCRIPTION_ELEMENT_NAME);
                DescriptionXElement.Value = i_Description;
                m_XElement.Add(DescriptionXElement);
            }

            m_Sync = new Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Sync
                (
                i_XDocument,
                i_By,
                i_ID
                );

            if (m_XElement.HasElements)
                m_XElement.FirstNode.AddBeforeSelf(m_Sync.XElement);
            else
                m_XElement.Add(m_Sync.XElement);

            this.Validate();
        }

        public RSSFeedSyncItem(System.Xml.Linq.XElement i_XElement)
        {
            this.Initialize(i_XElement);
        }

        public string Title
        {
            get
            {
                return m_Title;
            }
            set
            {
                System.Xml.Linq.XElement TitleXElement = m_XElement.Element(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_TITLE_ELEMENT_NAME);
                if (TitleXElement != null)
                    TitleXElement.Remove();

                if (!System.String.IsNullOrEmpty(value))
                {
                    TitleXElement = new System.Xml.Linq.XElement(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_TITLE_ELEMENT_NAME);
                    m_XElement.Add(TitleXElement);
                    TitleXElement.Value = value;
                }

                m_Title = value;
            }
        }

        public string Description
        {
            get
            {
                return m_Description;
            }
            set
            {
                System.Xml.Linq.XElement DescriptionXElement = m_XElement.Element(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_DESCRIPTION_ELEMENT_NAME);
                if (DescriptionXElement != null)
                    DescriptionXElement.Remove();

                if (!System.String.IsNullOrEmpty(value))
                {
                    DescriptionXElement = new System.Xml.Linq.XElement(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_DESCRIPTION_ELEMENT_NAME);
                    m_XElement.Add(DescriptionXElement);
                    DescriptionXElement.Value = value;
                }

                m_Description = value;
            }
        }

        public override void Update(System.DateTime? i_WhenDateTime, string i_By)
        {
            this.Validate();

            m_Sync.Update(i_WhenDateTime, i_By, true);
        }

        public override void ResolveConflicts(System.DateTime? i_WhenDateTime, string i_By)
        {
            this.Validate();

            m_Sync.ResolveConflicts(i_WhenDateTime, i_By, true);
        }

        public override void Delete(System.DateTime? i_WhenDateTime, string i_By)
        {
            this.Validate();

            m_Sync.Delete(i_WhenDateTime, i_By, true);
        }

        public override Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Sync Initialize(System.Xml.Linq.XElement i_XElement)
        {
            base.Initialize(i_XElement);

            System.Xml.Linq.XElement m_TitleXElement = m_XElement.Element(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_TITLE_ELEMENT_NAME);
            if (m_TitleXElement != null)
                m_Title = m_TitleXElement.Value;

            System.Xml.Linq.XElement m_DescriptionXElement = m_XElement.Element(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_DESCRIPTION_ELEMENT_NAME);
            if (m_DescriptionXElement != null)
                m_Description = m_DescriptionXElement.Value;

            this.Validate();

            return m_Sync;
        }

        private void Validate()
        {
            if (System.String.IsNullOrEmpty(m_Title) && System.String.IsNullOrEmpty(m_Description))
                throw new System.ArgumentException("m_Title and m_Description cannot both be null!");

            bool InvalidElement =
                (m_XElement.Name.LocalName != Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_ITEM_ELEMENT_NAME) ||
                (!System.String.IsNullOrEmpty(m_XElement.Name.NamespaceName));

            if (InvalidElement)
                throw new System.ArgumentException("Invalid m_XElement!");

            if (m_Sync == null)
                throw new System.ArgumentException("m_Sync must be non-null!");
        }

    }
}
